package ro.msg.quiz.view;

import java.awt.Font;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;

import ro.msg.quiz.controller.Controller;
import ro.msg.quiz.repository.Question;

/**
 * Create the panel.
 * 
 * @author Adorjan
 */
public class AddQuizSelectQuestions extends JPanel {
	private static int MAXNO_QUESTIONS = 10000000;
	private final String[] columns = { "Add", "Questions" };
	private final JTextField textField;
	private final Controller ctrl;
	private final JTable table;
	private int noOfQuestions;

	/**
	 * The panel's constructor
	 * 
	 * @author Adorjan
	 */
	public AddQuizSelectQuestions(Controller controller,
			ArrayList<Integer> questionIds) {
		this.ctrl = controller;
		setLayout(null);

		JLabel lblPleaseSelectThe = new JLabel(
				"Please select the questions to add to this questionnaire");
		lblPleaseSelectThe.setFont(new Font("Tahoma", Font.PLAIN, 12));
		lblPleaseSelectThe.setBounds(32, 22, 324, 14);
		add(lblPleaseSelectThe);

		JLabel lblSelect = new JLabel("Select");
		lblSelect.setFont(new Font("Tahoma", Font.PLAIN, 12));
		lblSelect.setBounds(32, 241, 34, 14);
		add(lblSelect);

		textField = new JTextField();
		/**
		 * Random number textfield's action listener
		 */
		textField.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				if (!textField.getText().toString().equals("")) {
					int tempNum = Integer.parseInt(textField.getText()
							.toString());
					if (tempNum > MAXNO_QUESTIONS)
						e.consume();
				}
				char caracter = e.getKeyChar();
				if (((caracter < '0') || (caracter > '9'))) {
					e.consume();
				}
			}
		});
		textField.setBounds(76, 239, 42, 20);
		add(textField);
		textField.setColumns(10);

		JLabel lblQuestionsRandomly = new JLabel("questions randomly");
		lblQuestionsRandomly.setFont(new Font("Tahoma", Font.PLAIN, 12));
		lblQuestionsRandomly.setBounds(128, 241, 105, 14);
		add(lblQuestionsRandomly);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(32, 57, 347, 173);
		add(scrollPane);

		table = new JTable();
		scrollPane.setViewportView(table);
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table.setModel(new DefaultTableModel(new Object[][] {}, new String[] {
				"Add", "Questions", "ID" }) {
			Class[] columnTypes = new Class[] { Boolean.class, String.class,
					Integer.class };

			@Override
			public Class getColumnClass(int columnIndex) {
				return columnTypes[columnIndex];
			}

			@Override
			public boolean isCellEditable(int row, int column) {
				if (column == 1)
					return false;
				return true;
			}
		});

		table.getColumnModel().getColumn(0).setPreferredWidth(46);
		table.getColumnModel().getColumn(1).setPreferredWidth(219);
		// hide the ID column
		table.removeColumn(table.getColumnModel().getColumn(2));
		/**
		 * table's table model listener
		 */
		table.getModel().addTableModelListener(new TableModelListener() {

			@Override
			public void tableChanged(TableModelEvent e) {
				noOfQuestions = 0;
				for (int i = 0; i < table.getModel().getRowCount(); i++) {
					if ((Boolean) table.getModel().getValueAt(i, 0)) {
						noOfQuestions++;
					}
				}
			}
		});

		fillModel();
		initSelection(questionIds);
	}

	/**
	 * fills the table "table" with questions from the repository
	 */
	private void fillModel() {
		DefaultTableModel dtm = (DefaultTableModel) table.getModel();
		int repoLength = ctrl.getQuestionList().size();
		for (int i = 0; i < repoLength; i++) {
			Question question = ctrl.getQuestionList().get(i);
			dtm.addRow(new Object[] { false, question.getText().toString(),
					question.getId() });
		}
	}

	/**
	 * @return the noOfQuestions
	 */
	public int getNoOfQuestions() {
		return noOfQuestions;
	}

	/**
	 * @return the textField
	 */
	public JTextField getTextField() {
		return textField;
	}

	/**
	 * @return the table
	 */
	public JTable getTable() {
		return table;
	}

	/**
	 * checks in the questions which were already selected
	 * 
	 * @param questionIds
	 */
	private void initSelection(ArrayList<Integer> questionIds) {
		for (Integer qId : questionIds) {
			table.setValueAt(true, qId, 0);
		}
	}
}
